﻿/////////////////////////////////////////////////////////////////////////////////////////////
// <copyright file="OutlookLiveUser.cs" company="Microsoft Corporation">
//     Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
/////////////////////////////////////////////////////////////////////////////////////////////

namespace Microsoft.LiveAtEdu.Samples.Powershell.OutlookLive
{
    using System;
    using System.Collections.ObjectModel;
    using System.Management.Automation;

    /// <summary>
    /// Powershell commands to manage user accounts in OutlookLive.
    /// </summary>
    public class OutlookLiveUser
    {
                /// <summary>
        /// Session runspace instance.
        /// </summary>
        private readonly SessionRunspace sessionRunspace;

        /// <summary>
        /// Initializes a new instance of the OutlookLiveUser class.
        /// </summary>
        /// <param name="sessionRunspace">Session runspace instance.</param>
        public OutlookLiveUser(SessionRunspace sessionRunspace)
        {
            if (sessionRunspace == null)
            {
                throw new ArgumentOutOfRangeException("sessionRunspace");
            }

            this.sessionRunspace = sessionRunspace;
        }

        /// <summary>
        /// Method to change user address
        /// </summary>
        /// <param name="identity">Windows live id</param>
        /// <param name="streetAddress">Street address</param>
        /// <param name="city">Name of city</param>
        /// <param name="state">Name of state</param>
        /// <param name="country">Name of country</param>
        /// <param name="postalCode">Postal code</param>
        /// <param name="mobile">Mobile number</param>
        /// <param name="title">User's title</param>
        /// <returns>true if success</returns>
        public bool ChangeUserAddr(string identity, string streetAddress, string city, string state, string country, string postalCode, string mobile, string title)
        {
            try
            {
                var command = new PSCommand();
                command.AddScript(string.Format(UserScripts.ChangeUserAddrScript, identity, streetAddress, city, state, country, postalCode, mobile, title));
                this.sessionRunspace.ExecuteCommand(command);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Get collection of User Ids.
        /// </summary>
        /// <returns>UserInfo object containing user information.</returns>
        public Collection<UserInfo> FetchUsers()
        {
            var command = new PSCommand();
            command.AddScript(UserScripts.GetUser);

            var objectList = this.sessionRunspace.ExecuteCommand(command);

            var userInfoList = new Collection<UserInfo>();
            foreach (PSObject obj in objectList)
            {
                if (!string.IsNullOrEmpty(obj.Members[Constants.UserPrincipalName].Value.ToString()))
                {
                    userInfoList.Add(new UserInfo(obj.Members[Constants.UserPrincipalName].Value.ToString()));
                }
            }

            return userInfoList;
        }
    }
}
